
typedef unsigned int16  i2c_add_t;
#define hi(x)  (*((int8 *)&x+1))
#define I2C_TIMEOUT_DRV (5)
#define RD_ADR(a) ((a)|0x01)

int1 ext_i2c_dev_ready(int8 chip_add)
{
    int1 ack;
    i2c_start();                // If the write command is acknowledged
    ack = i2c_write(chip_add);  // then the device is ready
    i2c_stop();
    return !ack;
}


int1 check_i2c_dev (int8 dev_adr, int8 time_out)
{
    do {
        if (! time_out--) {
            return 0;
        }
    } while(!ext_i2c_dev_ready(dev_adr));

    return 1;
}


int8 write_i2c_dev(int8 chip_add, i2c_add_t address, int8 *data)
{
    int8 cnt = I2C_TIMEOUT_DRV;
    do {
        if (! cnt--) {
            //printf ("WR - i2c ready time out\r\n");
            return -1;
        }
    } while (!ext_i2c_dev_ready(chip_add));
        
    i2c_start();
    i2c_write(chip_add);
    
#if (2 == I2C_ADR_SIZE)
    i2c_write(hi(address));
#endif
    i2c_write(address);
    i2c_write(*data);
    i2c_stop();

    return 0;
}


int8 read_i2c_dev(int8 chip_add, i2c_add_t address, int8 *data)
{
    int8 cnt = I2C_TIMEOUT_DRV;
    do {
        if (! cnt--) {
            //printf ("RD - i2c ready time out\r\n");
            return -1;
        }
    } while (!ext_i2c_dev_ready(chip_add));

    i2c_start();
    i2c_write(chip_add);

#if (2 == I2C_ADR_SIZE)
    i2c_write(hi(address));
#endif

    i2c_write(address);
    i2c_start();
    i2c_write(RD_ADR(chip_add));

    delay_us(20);
    //while (!i2c_poll())

    *data = i2c_read(0);
    i2c_stop();

    return 0;
}

// TODO: not work with pic slave 
int8 write_i2c_dev_block(int8 chip_add, i2c_add_t address, int8 *data,
                                                                int16 size)
{
    int8 cnt = I2C_TIMEOUT_DRV;
    do {
        if (! cnt--) {
            //printf ("WR - i2c ready time out\r\n");
            return -1;
        }
    } while (!ext_i2c_dev_ready(chip_add));
        
    i2c_start();
    i2c_write(chip_add);

#if (2 == I2C_ADR_SIZE)
    i2c_write(hi(address));
#endif

    i2c_write(address);

    /* write data buffer */
    while (size --) {
     //   i2c_start();
        i2c_write(*data);
        data ++;
    }
    
    i2c_stop();

    return 0;
}

// TODO: not work with pic slave 
int8 read_i2c_dev_block(int8 chip_add, i2c_add_t address, int8 *data,
                                                                int16 size)
{
    int8 cnt = I2C_TIMEOUT_DRV;
    do {
        if (! cnt--) {
            //printf ("RD - i2c ready time out\r\n");
            return -1;
        }
    } while (!ext_i2c_dev_ready(chip_add));

    i2c_start();
    i2c_write(chip_add);
    i2c_write(hi(address));
    i2c_write(address);
    i2c_start();
    i2c_write(RD_ADR(chip_add));

    /* read data buffer */
    while (size --) {
        delay_us(15);
        *data=i2c_read(1);	//remove loopreadin test 
        //*data = i2c_read(0);
        data ++;
    }

    i2c_read(0);
    i2c_stop();

    return 0;
}

